<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>离线页面 - 精英杂志</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #333;
            padding: 20px;
        }

        .offline-container {
            background: white;
            border-radius: 20px;
            padding: 40px;
            text-align: center;
            max-width: 400px;
            width: 100%;
            box-shadow: 0 20px 60px rgba(0,0,0,0.1);
            animation: slideUp 0.6s ease-out;
        }

        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .offline-icon {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, #ff6b6b, #ee5a24);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 20px;
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }

        .offline-icon::before {
            content: '⚠️';
            font-size: 40px;
        }

        h1 {
            color: #2c3e50;
            margin-bottom: 15px;
            font-size: 24px;
            font-weight: 600;
        }

        p {
            color: #7f8c8d;
            line-height: 1.6;
            margin-bottom: 25px;
            font-size: 16px;
        }

        .offline-content {
            background: #f8f9fa;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 25px;
        }

        .offline-content h3 {
            color: #495057;
            font-size: 18px;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .offline-features {
            list-style: none;
            text-align: left;
        }

        .offline-features li {
            padding: 8px 0;
            color: #6c757d;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .offline-features li::before {
            content: '✅';
            flex-shrink: 0;
        }

        .retry-button {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .retry-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
        }

        .retry-button:active {
            transform: translateY(0);
        }

        .status-indicator {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background: #e3f2fd;
            color: #1976d2;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 14px;
            margin-top: 20px;
            font-weight: 500;
        }

        .status-indicator::before {
            content: '';
            width: 8px;
            height: 8px;
            background: #ff9800;
            border-radius: 50%;
            animation: blink 1.5s infinite;
        }

        @keyframes blink {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.3; }
        }

        .cached-articles {
            margin-top: 20px;
            padding: 15px;
            background: #e8f5e8;
            border-radius: 8px;
            border-left: 4px solid #4caf50;
        }

        .cached-articles h4 {
            color: #2e7d32;
            font-size: 16px;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .cached-list {
            list-style: none;
            font-size: 14px;
        }

        .cached-list li {
            padding: 4px 0;
            color: #388e3c;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .cached-list li::before {
            content: '📄';
        }

        /* 移动端适配 */
        @media (max-width: 480px) {
            .offline-container {
                padding: 30px 20px;
                margin: 10px;
            }

            h1 {
                font-size: 20px;
            }

            p {
                font-size: 14px;
            }

            .offline-icon {
                width: 60px;
                height: 60px;
            }

            .offline-icon::before {
                font-size: 30px;
            }
        }

        /* 深色模式支持 */
        @media (prefers-color-scheme: dark) {
            body {
                background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            }

            .offline-container {
                background: #2c3e50;
                color: #ecf0f1;
            }

            h1 {
                color: #ecf0f1;
            }

            p {
                color: #bdc3c7;
            }

            .offline-content {
                background: #34495e;
            }

            .offline-content h3 {
                color: #ecf0f1;
            }

            .offline-features li {
                color: #bdc3c7;
            }

            .status-indicator {
                background: #34495e;
                color: #3498db;
            }

            .cached-articles {
                background: #27ae60;
                color: #ecf0f1;
            }

            .cached-articles h4 {
                color: #ecf0f1;
            }

            .cached-list li {
                color: #ecf0f1;
            }
        }
    </style>
</head>
<body>
    <div class="offline-container">
        <div class="offline-icon"></div>
        <h1>网络连接中断</h1>
        <p>您当前处于离线状态，但别担心！精英杂志支持离线阅读功能。</p>
        
        <div class="offline-content">
            <h3>📱 离线功能</h3>
            <ul class="offline-features">
                <li>已缓存的文章可正常阅读</li>
                <li>支持离线收藏和笔记</li>
                <li>自动同步阅读进度</li>
                <li>智能图片压缩节省流量</li>
            </ul>
        </div>

        <button class="retry-button" onclick="checkConnection()">
            <span>🔄</span>
            <span>重新连接</span>
        </button>

        <div class="status-indicator">
            <span>正在等待网络连接...</span>
        </div>

        <div id="cached-articles" class="cached-articles" style="display: none;">
            <h4>📚 可阅读的缓存文章</h4>
            <ul id="cached-list" class="cached-list"></ul>
        </div>
    </div>

    <script>
        // 检查网络连接
        function checkConnection() {
            if (navigator.onLine) {
                window.location.reload();
            } else {
                // 显示错误提示
                const button = document.querySelector('.retry-button');
                const originalText = button.innerHTML;
                button.innerHTML = '<span>❌</span><span>仍无连接</span>';
                button.style.background = '#f44336';
                
                setTimeout(() => {
                    button.innerHTML = originalText;
                    button.style.background = 'linear-gradient(135deg, #667eea, #764ba2)';
                }, 2000);
            }
        }

        // 显示缓存的文章
        async function showCachedArticles() {
            try {
                const cache = await caches.open('elite-magazine-api');
                const response = await cache.match('/index.json');
                
                if (response) {
                    const articles = await response.json();
                    const cachedContainer = document.getElementById('cached-articles');
                    const cachedList = document.getElementById('cached-list');
                    
                    if (articles.length > 0) {
                        cachedContainer.style.display = 'block';
                        cachedList.innerHTML = articles.slice(0, 5).map(article => 
                            `<li>${article.title}</li>`
                        ).join('');
                    }
                }
            } catch (error) {
                console.log('无法加载缓存文章:', error);
            }
        }

        // 监听网络状态变化
        window.addEventListener('online', () => {
            window.location.reload();
        });

        window.addEventListener('offline', () => {
            console.log('网络已断开');
        });

        // 页面加载时显示缓存文章
        document.addEventListener('DOMContentLoaded', () => {
            showCachedArticles();
            
            // 定期检查网络状态
            setInterval(checkConnection, 5000);
        });

        // 添加页面可见性变化监听
        document.addEventListener('visibilitychange', () => {
            if (!document.hidden && navigator.onLine) {
                window.location.reload();
            }
        });
    </script>
</body>
</html>